#include "chooselevelscene.h"
#include <QPainter>
#include <qmenubar.h>
#include "mypushbutton.h"
#include   <QDebug>
#include <QLabel>
#include <QSound>
#include <QTimer>
Chooselevelscene::Chooselevelscene(QWidget *parent) : QMainWindow(parent)
{

    //音效资源加载
    QSound * startSound = new QSound(":/res/TapButtonSound.wav",this);
    QSound * backSound = new QSound(":/res/BackButtonSound.wav",this);

    //设置固定大小
    setFixedSize(320,588);
    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    //设置标题
    setWindowTitle("关卡选择");
    //创建菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);
    //创建开始菜单栏
    QMenu * startMenu = bar->addMenu("开始");
    //创建退出 菜单项
    QAction *quitAction = startMenu->addAction("退出");

    //点击退出 实现退出游戏
    connect(quitAction,&QAction::triggered,[=]() {
        this->close();
    });
    //返回按钮
    myPushButton * backBtn = new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    //定位右下角
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    connect(backBtn,&myPushButton::clicked,[=]() {
        //返回音效
        backSound->play();
        //告诉主场景返回信号,主场景监听ChooseLevelScene的返回按钮
        QTimer::singleShot(200,this,[=]() {
            emit this->chooseSceneBack();
        });
    });
    //创建关卡选择按钮
    for(int i =0 ; i< 20 ; i++) {
        //通过一个循环画出矩阵
        myPushButton * menuBtn = new myPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+i%4 * 70,130+i/4 *70);
        //显示按钮上的文字
        QLabel * label = new QLabel ;
        label->setParent(this);
        //通常为了对其label设置和图片大小一致
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        //设置文字水平居中,图片本身是不规则的
        label->setAlignment(Qt::AlignCenter);
        label->setText(QString::number(i+1));
        label->move(25+i%4 * 70,130+i/4 *70);
        //监听每个按钮的点击事件
        connect(menuBtn,&myPushButton::clicked,this,[=]() {
            //选关音效
            startSound->play();
            QString str = QString ("您选择的是%1关").arg(i+1);
            qDebug()<<str;

            this->hide();
            //进入到游戏场景
            playScene =  new PlayScene(i+1);//创建游戏场景
            //位置还原,修复位置没有保留的bug
           playScene->setGeometry(this->geometry());
            //显示游戏场景
            playScene->show();
            connect(playScene,&PlayScene::chooseSceneBack,this,[=](){
                 //位置还原,修复位置没有保留的bug
                this->setGeometry(playScene->geometry());
                this->show();
                //游戏场景直接销毁置空
                delete playScene;
                playScene = NULL;
            });
        });
        //设置按键图层穿透,不然label标签在按钮上 设置label图层穿透 51号属性
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    };
}

//背景画面
void Chooselevelscene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap  pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题
    pix.load(":/res/Title.png");
    //这里调整的是正好容得下pix的起点
    painter.drawPixmap((this->width()-pix.width())*.5,30,pix.width(),pix.height(),pix);

}


